package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.pojo.Gift;
import com.water.bs.service.GiftManageService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
public class GiftManageController {
    @Autowired
    private GiftManageService giftManageService;
    @RequestMapping("/getGiftList")
    public JSON getGiftList(){
        JSONObject rs = new JSONObject();
        List<Gift> gifts = giftManageService.queryAllGift();
        System.out.println("礼物列表数量:"+gifts.size());
        if (gifts.size()>0){
            rs.put("state","0");
            rs.put("message","获取成功!");
            rs.put("data",gifts);
            return  rs;
        }else{
            rs.put("state","1");
            rs.put("message","获取礼物列表失败!");
            return  rs;
        }
    }
    @RequestMapping("/getNoPhotoGiftList")
    public JSON getNoPhotoGiftList(){
        JSONObject rs = new JSONObject();
        List<Gift> gifts = giftManageService.queryNoPhotoGift();
        if (gifts.size()>0){
            rs.put("state","0");
            rs.put("message","获取成功");
            rs.put("data",gifts);
            return  rs;
        }else{
            rs.put("state","1");
            rs.put("message","暂时没有未添加图片的礼物~");
            return  rs;
        }
    }


    @RequestMapping("/deleteGift")
    public JSON deleteGift(@Param("id")String id){
        System.out.println("获取需要删除的礼物id:"+id);
        JSONObject rs = new JSONObject();
        int i = giftManageService.deleteGift(id);
        if (i>0){
            rs.put("state","0");
            rs.put("message","删除成功!");
            return  rs;
        }else{
            rs.put("state","1");
            rs.put("message","删除礼物失败!");
            return  rs;
        }
    }
    @RequestMapping("/editGift")
    public JSON editGift (@RequestBody JSONObject editGiftInfo){
        JSONObject rs = new JSONObject();
        System.out.println("接收到修改后的礼物信息"+editGiftInfo);
        Map<String,Object> map = new HashMap<>();
        map.put("id",editGiftInfo.get("id") );
        if (editGiftInfo.get("name")!=null){
            map.put("name",editGiftInfo.get("name") );
        }
        if (editGiftInfo.get("price")!=null){
            map.put("price",editGiftInfo.get("price"));
        }
        if (editGiftInfo.get("stock")!=null){
            map.put("stock",editGiftInfo.get("stock"));
        }
        int i = giftManageService.updateGift(map);
        if (i ==1){
            rs.put("state","0");
            rs.put("message","修改完成!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","礼物不存在或信息错误!");
            return rs;
        }
    }
    @RequestMapping("/addGift")
    public JSON savePhoto(@RequestBody JSONObject addGiftInfo){
        JSONObject rs = new JSONObject();
        Map<String,Object> map = new HashMap<>();
        map = addGiftInfo;
        map.put("id", UUID.randomUUID().toString());
        int i = giftManageService.addGift(map);
        if (i ==1){
            rs.put("state","0");
            rs.put("message","新增礼物成功，请尽快添加图片!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","添加失败!");
            return rs;
        }
    }
    @RequestMapping("/saveGiftPhoto")
    public JSON savePhoto(@RequestParam("file")MultipartFile file,@RequestParam("id") String id){
        JSONObject rs = new JSONObject();
        System.out.println(file);
        System.out.println(id);
        Map<String,Object> map = new HashMap<>();
        if (file.isEmpty()){
            rs.put("state","1");
            rs.put("message","上传失败,请重新选择!");
            return rs;
        }

        try {
            InputStream is = file.getInputStream();
            byte[] photoFile = new byte[(int)file.getSize()];
            System.out.println(file.getSize());
            is.read(photoFile);

            map.put("photofile",photoFile);
            map.put("id",id);

            int i = giftManageService.saveGiftPhoto(map);
            if (i>0){
                rs.put("state","0");
                rs.put("message","保存成功");
            }else{
                rs.put("state","1");
                rs.put("message","上传失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rs;

    }

    @RequestMapping("/getGiftPhoto")
    public void getPhoto(@RequestParam("id")String id,final HttpServletResponse response) throws IOException {
        Gift gift = giftManageService.getGiftPhoto(id);
        byte[] data = gift.getPhotoFile();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputStream = response.getOutputStream();
        InputStream in = new ByteArrayInputStream(data);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();

    }

    @RequestMapping("/qryAllGiftHistory")
    public JSON qryAllGiftHistory(){
        JSONObject rs = new JSONObject();
        rs.put("data",giftManageService.qryAllGiftHistory());
        return rs;
    }

    @RequestMapping("/saveGiftExpress")
    public JSON saveGiftExpress(@RequestBody JSONObject expressInfo){
        System.out.println("保存物流信息:"+expressInfo);
        //格式 保存物流信息:{"id":"ASFCOI234","expressid":"123"}
        JSONObject rs = new JSONObject();
        int i = giftManageService.saveGiftExpress(expressInfo);
        if (i >0){
            rs.put("state","0");
            rs.put("message","操作成功!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
            return rs;
        }
    }
}
